package com.guangzhi.weijianzhi.company.manager;

import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.gson.JsonObject;
import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.activity.BaseActivity;
import com.guangzhi.weijianzhi.adapter.MyArrayApterOne;
import com.guangzhi.weijianzhi.company.manager.PublishPositionActivity.CloseReceiver;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.utils.Misc;
import com.guangzhi.weijianzhi.utils.PopuUtils;

public class PublishPositionThreeActivity extends BaseActivity implements OnClickListener {

	private TextView mService;
	private EditText mCenter;
	private TextView mName;
	private TextView mPhone;
	private EditText mNumber;
	private CheckBox mCheck;
	private TextView isGong;
	private View view_isgong;
	private String service_type ="0";
	 private CloseReceiver closeReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.tz_publish_positon_three_layout);
		
		initView();
		onregistBroab();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(closeReceiver);
	}
	
	private void onregistBroab() {
		// TODO Auto-generated method stub
		closeReceiver = new CloseReceiver();  
		IntentFilter intentFilter = new IntentFilter("com.guangzhi.weijianzhi");  
		registerReceiver(closeReceiver, intentFilter);  
	}
	   /** 
	    * 结束Activity的广播接收机 
	    * @author shuxiang 
	    * 
	    */  
	   public class CloseReceiver extends BroadcastReceiver  
	{   
	       @Override  
	       public void onReceive(Context context, Intent intent)  
	       {  
	    	   PublishPositionThreeActivity.this.finish();  
	       }  
	   } 
	private void initView() {
		// TODO Auto-generated method stub
		initTitle();
		goBack.setOnClickListener(this);
		titleTextV.setText("发布职位");
		mCenter = (EditText) findViewById(R.id.center);
		mName = (TextView) findViewById(R.id.name);
		mPhone = (TextView) findViewById(R.id.phone);
		mService = (TextView) findViewById(R.id.service);
		mService.setOnClickListener(this);
		mNumber = (EditText) findViewById(R.id.day_bunber);
		mCheck = (CheckBox) findViewById(R.id.check);
		isGong = (TextView) findViewById(R.id.text_isgong);
		view_isgong = findViewById(R.id.view_isgong);
		TextView pub_not = (TextView) findViewById(R.id.publish_notice);
		pub_not.setText(Html.fromHtml("<u>" + "发布须知" + "</u>"));
		findViewById(R.id.finish).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.service:
			
			showService();
			
			break;
		case R.id.finish:
			
			if(isFinish()){
				submitDate();
			}
			
			break;
		case R.id.title_back:
			onBackPressed();
			break;
		}
		
	}

	private void submitDate() {
		HttpRequestUtils.doHttpPublishPosition(mCenter.getText().toString(), mName.getText().toString(),
				mPhone.getText().toString(), mNumber.getText().toString(), 
        service_type, new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				
				try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					if(jsonO.optBoolean("status")){
						
						if(!Misc.notNull(mNumber.getText().toString()) ||"0".equals(mNumber.getText().toString())){
							Misc.alert("职位发布成功,等待审核");
							Intent intent = new Intent();  
							intent.setAction("com.guangzhi.weijianzhi");  
							sendBroadcast(intent); 
						}else{
							Intent intent = new Intent(PublishPositionThreeActivity.this, PaySelectActivity.class);
							intent.putExtra("date", new String(arg2));
							startActivity(intent);
						}
						

						
					}else{
						Misc.alert(jsonO.optString("errMessage"));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			};
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
			}
		});
	}

	private boolean isFinish() {
		// TODO Auto-generated method stub
		if(!Misc.notNull(mCenter.getText().toString())){
			Misc.alert("请填写工作内容");
			return false;
		}else if(!Misc.notNull(mName.getText().toString())){
			Misc.alert("请填写联系人姓名");
			return false;
		}else if(!Misc.notNull(mPhone.getText().toString())){
			Misc.alert("请填写联系人电话");
			return false;
		}else if(!Misc.notNull(mPhone.getText().toString())){
			Misc.alert("请填写联系人电话");
			return false;
		}else if(!mCheck.isChecked()){
			Misc.alert("请同意发布须知");
			return false;
		}
		else{
		if(Misc.notNull(mService.getText().toString())&&!"不需要".equals(mService.getText().toString())){
				if(Misc.notNull(mNumber.getText().toString())){
					return true;
				}else{
					Misc.alert("请填写服务天数");
					return false;
				}
			}else{
				return true;
			}
			
		}
	}

	private void showService() {
		// TODO Auto-generated method stub
		final ArrayList<String> arrays = new ArrayList<String>();
		arrays.add("不需要");arrays.add("推荐 20/天");
		arrays.add("置顶 30/天");arrays.add("全国可见 50/天");
		
		// TODO Auto-generated method stub
		View view = View.inflate(this, R.layout.tz_pop_list2_layout, null);
		ListView list = (ListView) view.findViewById(R.id.one_list);
		final PopupWindow showPou = PopuUtils.showPou(View.inflate(this, R.layout.tz_company_info_layout, null), view, this, this);
		MyArrayApterOne array = new MyArrayApterOne(arrays, this);
		list.setAdapter(Misc.getMyadapter(array, list));
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				showPou.dismiss();
				service_type = arg2+"";
				if(arg2 ==0){
					isGong.setVisibility(View.GONE);
					mNumber.setVisibility(View.GONE);
				}else{
					isGong.setVisibility(View.VISIBLE);
					mNumber.setVisibility(View.VISIBLE);
				}
				mService.setText(arrays.get(arg2));
			}
		});
	}
}
